package com.tpyule.tpclass.web.Controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tpyule.tpclass.entity.bean.FailResultBean;
import com.tpyule.tpclass.entity.bean.ResultBean;
import com.tpyule.tpclass.entity.bean.SuccessResultBean;
import com.tpyule.tpclass.entity.bo.AnswerDetailBo;
import com.tpyule.tpclass.entity.dto.AnswerQueryDto;
import com.tpyule.tpclass.entity.pojo.AD;
import com.tpyule.tpclass.entity.pojo.Answer;
import com.tpyule.tpclass.service.ADService;
import com.tpyule.tpclass.service.AnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author zzz
 * @version 1.0
 * @date 2020/4/26 21:47
 */
@RestController
@RequestMapping("/ad")
public class ADController extends BaseController {
    @Autowired
    ADService adService;
    @RequestMapping("/add")
    @ResponseBody
    public void addAd(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String adData = request.getParameter("adData");
            if (adData!=null){
                AD ad = objectMapper.readValue(adData, AD.class);
                if (ad!=null){
                    int result = adService.addAD(ad);
                    if (result!=0){
                        resultBean = new SuccessResultBean();
                    } else {
                        resultBean = new FailResultBean();
                    }
                } else {
                    resultBean = new FailResultBean("数据错误");
                }
            } else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response, resultBean);
        }
    }

    @RequestMapping("/updateAD")
    @ResponseBody
    public void updateAd(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String adData = request.getParameter("adData");
            if (adData!=null){
                AD ad = objectMapper.readValue(adData, AD.class);
                if (ad!=null){
                    int result =adService.updateAD(ad);
                    if (result!=0){
                        resultBean = new SuccessResultBean();
                    } else {
                        resultBean = new FailResultBean();
                    }
                } else {
                    resultBean = new FailResultBean("数据错误");
                }
            } else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response, resultBean);
        }
    }

    @RequestMapping("/deleteAD")
    @ResponseBody
    public void deleteAD(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String adData = request.getParameter("adData");
            if (adData!=null){
                AD ad = objectMapper.readValue(adData, AD.class);
                if (ad!=null){
                    int result =adService.deleteAD(ad);
                    if (result!=0){
                        resultBean = new SuccessResultBean();
                    } else {
                        resultBean = new FailResultBean();
                    }
                } else {
                    resultBean = new FailResultBean("数据错误");
                }
            } else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response, resultBean);
        }
    }


    @RequestMapping("/getAD")
    @ResponseBody
    public void getAD(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String id = request.getParameter("id");
            if (id!=null){
                int i = Integer.getInteger(id);
                AD ad = adService.getAD(i);
                if (ad!=null){
                    resultBean= new SuccessResultBean(ad,"成功");
                }else{
                    resultBean = new FailResultBean("数据错误");
                }
            } else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response, resultBean);
        }
    }


    @RequestMapping("/getListAD")
    @ResponseBody
    public void getListAD(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            List<AD> ads = adService.getListAD();
            if(ads!=null){
                resultBean = new SuccessResultBean(ads,"success");
            }else {
                resultBean = new FailResultBean();
            }
        } catch (Exception e) {
            resultBean = new FailResultBean();
        } finally {
            this.setAjaxResponseResult(response, resultBean);
        }
    }

}
